package edu.cmu.cs.stage3.alice.core.property;

import java.io.IOException;

import edu.cmu.cs.stage3.alice.core.Element;
import edu.cmu.cs.stage3.alice.core.ReferenceGenerator;
import edu.cmu.cs.stage3.alice.core.applet.AppletXmlStruct;
import edu.cmu.cs.stage3.io.DirectoryTreeStorer;

public class BooleanProperty extends ObjectProperty {

 public BooleanProperty (Element owner, String name, Boolean defaultValue) {
  super( owner, name, defaultValue, Boolean.class );
  }

 //Rem @Override
 public Object getValue () {
  Object value = super.getValue();
  if( value instanceof Number ) {
   Number number = (Number)value;
   if (number.byteValue()!=0) {
    value = Boolean.TRUE;
   } else {
    value = Boolean.FALSE;
    }
   }
  return value;
  }

 public Boolean getBooleanValue () {
  return (Boolean)getValue();
  }

 public boolean booleanValue (boolean valueIfNull) {
  Boolean b = getBooleanValue();
  if (b != null) {
   return b.booleanValue();
  } else {
   return valueIfNull;
   }
  }

 public boolean booleanValue () {
  return booleanValue( false );
  }

 //Rem @Override
 protected void decodeObject (org.w3c.dom.Element node, java.util.Vector referencesToBeResolved) throws java.io.IOException {
  set( Boolean.valueOf( getNodeText( node ) ) );
  }

 //Rem @Override
 protected void encodeObject (org.w3c.dom.Document document, org.w3c.dom.Element node, DirectoryTreeStorer storer, edu.cmu.cs.stage3.alice.core.ReferenceGenerator referenceGenerator) throws java.io.IOException {
  node.appendChild( createNodeForString( document, getBooleanValue().toString() ) );
  }

 //Rem @Override
 protected AppletXmlStruct encodeObjectToXml (ReferenceGenerator referenceGenerator) throws IOException {
  AppletXmlStruct struct = new AppletXmlStruct();
  struct.addElement(getBooleanValue().toString());
  return struct;
  }

 }
